<template>
	<view class="" >
		<view v-for="(item,index) in IndexData" :key="index">
			<!-- 搜索框组 -->
			<sc-search @search="searchTap" v-if="item.model_id == 4"></sc-search>
			<!-- 轮播组 -->
			<sc-banner v-if="item.model_id == 1" :detail="item.data" @navtive="navtive"></sc-banner>
			<!-- 占位组 -->
			<sc-placeholder v-if="item.model_id == 9" :detail="item.data" color="#ffffff"></sc-placeholder>
			<!-- 导航组 -->
			<sc-nav v-if="item.model_id == 2" :detail="item.data" @navtive="navtive"></sc-nav>
			<!-- 图片组 -->
			<sc-image v-if="item.model_id == 8" :detail="item.data" @navtive="navtive"></sc-image>
			<!-- 商城公告组 -->
			<sc-notice v-if="item.model_id == 3" :detail="item.data" @clickdetail="clickdetail" :dataArr="noticel" @notice='notice' @nav="nav('/pageD/noticList/noticList')"></sc-notice>
			<!-- 秒杀组 -->
			<sc-seckill v-if="item.model_id == 10 && skillType == 1" @nav="skillNav(1)" :skill="skill" @detailTap="detailTap"></sc-seckill>
			<!-- 优惠券组 -->
			<sc-coupon v-if="item.model_id == 6" :detail="item.data" @couponGet="couponGet($event,index)"></sc-coupon>
			<!-- 标题组 -->
			<sc-title v-if="item.model_id == 7" @navtive="navtive" :detail="item.data"></sc-title>
			<!-- 拼团组 -->
			<sc-tour v-if="item.model_id == 11 && assembleType == 1" :assemble="assemble" @detailTap="detailTap"></sc-tour>
			<!-- 商品组 -->
			<sc-like v-if="item.model_id == 5" :detail="item.data" @detailTap="detailTap"></sc-like>
		 
		</view>
	 
	
		<view class="" v-if="indexShow == 1">
			<sc-alltitle :detail="IndexTitle.data" :type="IndexTitle.type"></sc-alltitle>
			<!-- <sc-title @navtive="navtive" :detail="allType"></sc-title> -->
			<sc-like :detail="bottomList" @detailTap="detailTap"></sc-like>
			<uni-load-more :loadingType="loadingType" :contentText="contentText"></uni-load-more>
		</view>
		<shop-loading v-if="shopLoad"></shop-loading>
		<uni-login ref='login' @success="loginSuccess"></uni-login>
		<view class="" style="width: 100%;">
		 
			<footers></footers>
		</view>
	</view>
</template>

<script>
	import todayMustPush from '@/components/todayMustPush/todayMustPushList.vue'
	import recommendGoods from '@/components/indexRecommend/recommendList.vue'
	import newRecommendGoods from '@/components/newsRecommend/newsRecommendList.vue';
    import footers from '@/components/footer/footer.vue';
	import scBanner from '../components/sc-banner.vue';
	import scNotice from '../components/sc-notice.vue';
	import scImage from '../components/sc-image.vue';
	import scPlaceholder from '../components/sc-placeholder.vue';
	import scNav from '../components/sc-navigation.vue';
	import scTitle from '../components/sc-title.vue';
	import scSeckill from '../components/sc-seckill.vue';
	import scCoupon from '../components/sc-coupon.vue';
	import scTour from '../components/sc-tour.vue';
	import scLike from '../components/sc-like.vue';
	import scSearch from '../components/sc-search.vue';
	import scGuarantee from '../components/sc-guarantee.vue';
	import {Index,search,indexGoods,userCoupon} from '@/common/index.js';
	import Indexlist from '@/mixins/list.js';
	import uniLoadMore from '@/components/uni-load-more.vue';
	import scAlltitle from '../components/sc-alltitle.vue';
	export default {
		components: {
			todayMustPush,
			recommendGoods,
			newRecommendGoods,
			footers,
			scBanner,scNotice,scImage,scPlaceholder,scNav,scTitle,scSeckill,scCoupon,scTour,scLike,scSearch,scGuarantee,uniLoadMore,scAlltitle
		},
		data() {
			return {
				search_input:'',
				IndexData:[],
				noticel:[],
				skill:[],
				assemble:[],
				indexShow:0,
				bottomList:{
					type:2,
					list:[]
				},
				allType:{
					align: "center",
					bg: "",
					color: "#333",
					icon: "",
					more: false,
					name: "全部商品",
				},
				shopLoad:true,
				userCouponlist:[],
				assembleType:0,
				skillType:0,
				navtiveUrl:'',
				IndexTitle:{}
			}
		},
		mixins:[Indexlist],
		onLoad() {
							this.shopLoad = true;
			this.interData()
		},
		onShow() {
			this.$store.commit('CART_NUM');
		},
		onPullDownRefresh() {
			//下拉刷新的时候请求一次数据
			this.interData()
		},
		methods: {
			//登录成功回调
			loginSuccess(){
				const {navtiveUrl} = this;
				if(navtiveUrl){
					this.nav(navtiveUrl)
				}
			},
			login(){
				if(uni.getSystemInfoSync().version){
				   this.$refs.login.show = true;	
				}
			},
			couponGet(e,index){
				const {userCouponlist} = this;
				let id = e.id;
				let flag = true;
				// console.log(id , userCouponlist)
				userCouponlist.map(item=>{
					if(item === id){
						flag = false
					}
				});
				if(!flag){
					this.toast('您已经领取过了');
					return false;
				}
				if(e.total_num <= e.use_num){
					this.toast('该优惠券已经没有了');
					return false;
				}
				userCoupon({id}).then(res=>{
					if(res.code == 1){
						this.toast(res.msg)
					}else if(res.code == 4){
						this.toast(res.msg)
						this.login()
					}else{
						this.toast(res.msg)
					}
				}).catch()
				// console.log(e,index)
			},
			clickdetail(id){
				uni.navigateTo({
					url:'/pageD/noticDetail?id='+id
				})
			},
			//列表数据
			getData(){
				let params = {
					page:this.page
				}
				indexGoods(params).then(res=>{
					if(res.code == 1){
						const data = res.msg;
						if(data.data.length){
							this.bottomList.list = data.data
							this.loadingType = data.last_page > 1 ? 0 : 2;
							this.page++;
							uni.hideNavigationBarLoading();
							uni.stopPullDownRefresh();//得到数据后停止下拉刷新
						}else{
							uni.hideNavigationBarLoading();
							uni.stopPullDownRefresh();
						}
					}else{
						this.toast(res.msg)
					}
				}).catch()
			},
			getmoreNews(){
				if (this.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
				this.loadingType = 1;
				uni.showNavigationBarLoading();//显示加载动画
				const {page} = this;
				let params = {
					page
				}
				indexGoods(params).then(res=>{
					if(res.code == 1){
						const data = res.msg;
						if(data.data.length){
							this.bottomList.list = this.bottomList.list.concat(data.data)
							this.loadingType = data.last_page > 1 ? 0 : 2;
							this.page++;
							uni.hideNavigationBarLoading();
							this.loadflag = false;
						}else{
							this.loadingType = 2;
							uni.hideNavigationBarLoading();
						}
					}
				}).catch()
			},
			//商品跳转
			detailTap(item,type){
				const {skillType,assembleType} = this;
				if(type === '0' && skillType != 1){
					return false;
				}
				if(type === '3' && assembleType != 1){
					return false;
				}
				uni.navigateTo({
					url:'/pageB/classify/productdetail?type='+type+'&id='+item.id
				})
			},
			//首页数据
			interData(){
				Index({id:1}).then(res=>{
					if(res.code == 1){
						this.IndexData = res.msg.info;
						this.noticel = res.msg.notice;
						this.skill = res.msg.skill;
						let data = res.msg.assemble;
						this.assembleType = res.msg.assemble_type;
						this.skillType = res.msg.skill_type;
						let date = new Date();
						let now = date.getTime();
						data.forEach((item,index)=>{
							let da = (item.time*1000) - (item.now_time*1000);
							item.dateInfo = this.getDuration(da)
						})
						console.log(data)
						this.userCouponlist = res.msg.user_coupon
						// console.log('xaio',data)
						this.assemble = data;
						this.indexShow = res.msg.index_show;
						this.IndexTitle = res.msg.index_title
						this.shopLoad = false;
					}else{
						this.toast(res.msg)
						this.shopLoad = false;
					}
				}).catch()
			},
			getDuration(my_time) {
			  var days    = my_time / 1000 / 60 / 60 / 24;
			  var daysRound = Math.floor(days);
			  var hours = parseInt((my_time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			  var minutes = parseInt((my_time % (1000 * 60 * 60)) / (1000 * 60));
			  var seconds = parseInt(my_time % (1000 * 60)) / 1000;
			  // console.log('转换时间:', daysRound + '天', hoursRound + '时', minutesRound + '分', seconds + '秒');
			  
			  let dateInfo = {
				  day:daysRound,
				  h:hours,
				  m:minutes,
				  s:seconds
			  };
			  return dateInfo;
			},
			//组件跳转
			navtive(item){
				let type = item.model;
				switch (type){
					case 1:
					   this.nav('/pageB/classify/productdetail?type=1&id='+item.data.goods_id)
					   break;
					case 2:
					   this.nav('/pageB/classify/classProduct?id='+item.data.id)
					   break;
					case 3:
					   // this.nav('/pageA/seckill/seckill')
					   this.skillNav(1)
					   break;
					case 4:
					   this.skillNav(2)
					   // this.nav('/pageA/groupbooking/groupbooking')
					   break;
					case 5:
					   // this.nav('/pageD/searchList/searchList')
					   this.nav('/pageB/classify/classProduct?id=0')
					   break;
					case 6:
					   // this.nav('/pageD/searchList/searchList')
					   this.nav('/pageB/classify/classProduct?id=0')
					   break;
					case 7:
					   this.nav('/pageA/integral/integral')
					   break;
					case 8:
					   this.nav('/pageD/discount/discount')
					   break;
					case 9:
					   if(uni.getSystemInfoSync().version && !uni.getStorageSync('token')){
						   this.navtiveUrl = '/pageA/signin/signin'
						   this.login();
						   return false;
					   }
					   this.nav('/pageA/signin/signin')
					   break;
					case 10:
					   this.nav('/pages/classify/classify','type')
					   break;
					case 11:
					   if(uni.getSystemInfoSync().version && !uni.getStorageSync('token')){
						   this.navtiveUrl = '/pageA/balance/balance'
						   this.login();
						   return false;
					   }
					   this.nav('/pageA/balance/balance')
					   break;
					case 12:
					   this.nav('/pageD/account/voucherCenter')
					   break;
					case 13:
					   this.nav('/pageD/noticList/noticList')
					   break;
					default:''
					   break;
				}
			},
			skillNav(type){
				const {skillType,assembleType} = this;
				if(type == 1 && skillType == 1){
					this.nav('/pageA/seckill/seckill')	
				}else if(type == 2 && assembleType == 1){
					this.nav('/pageA/groupbooking/groupbooking')
				}
			},
			nav(item,type){
				if(!type){
					uni.navigateTo({
						url:item
					})
				}else{
					uni.switchTab({
						url:item
					})
				}
				
			},
			searchTap(){
				uni.navigateTo({
					url:'/pageD/searchList/searchList'
				})
			},
			//公告跳转
			notice(item){
				// uni.navigateTo({
				// 	url:item.url
				// })
				console.log(item)
			},
			clearInput(){
				this.search_input = ''
			}
			
		}
	}
</script>

<style lang="less" scoped>
	// @import  url('../../static/scss/index/index.scss')
	@import url('../../static/scss/index/index.less');

	// page {
	// 	background-color: #FFFFFF;
	// }
</style>
